// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    shareImage: '../../images/me.png',//背景图
    name: "用户昵称", //用户昵称
    headImg: "../../images/me.png", //用户头像
    erweima: "../../images/me.png", //动态二维码
    imgDraw: {}, // 绘制图片的大对象
    texta: "扫描二维码即可开通",
    texta1: "成为见习萌主哦",
    drawing: false,//重复保存图片
    imgUrl: ''//临时图片
  },
  onLoad() {
    wx.showShareMenu({
        // 要求小程序返回分享目标信息
        withShareTicket: true
    });
  },
  onShow() {
    const query=wx.createSelectorQuery()
       query.select('#myCanvas').fields({
        node:true,
        size:true
      }).exec(res=>{
        
        const {node}=res[0]
        if(!node){return;}
        const ctx=node.getContext('2d')
        console.log(ctx)
        ctx.fillStyle='black'
        ctx.font="normal 400 12px"
        ctx.fillText('1111',0,0)
      })
      
  },

  clickSaveImg() {
      var _this = this
      wx.canvasToTempFilePath({     //将canvas生成图片
          canvasId: 'myCanvas',
          x: 0,
          y: 0,
          width: 335,
          height: 472,
          success: function (res) {
              _this._data.imgUrl = res.tempFilePath
              wx.saveImageToPhotosAlbum({  //保存图片到相册
                  filePath: res.tempFilePath, //生成图片临时路径
                  success: function () {
                      wx.showToast({
                          title: "图片已保存至相册！",
                          duration: 2000
                      })
                  }
              })
          },
          fail: error => {
              wx.showToast({
                  title: "保存图片失败",
                  duration: 2000
              })
              if (error.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || error.errMsg === "saveImageToPhotosAlbum:fail auth deny" || error.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
                  // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
                  wx.showModal({
                      title: '提示',
                      content: '需要您授权保存相册',
                      showCancel: false,
                      success: modalSuccess => {
                          wx.openSetting({
                              success(settingdata) {
                                  console.log("settingdata", settingdata)
                                  if (settingdata.authSetting['scope.writePhotosAlbum']) {
                                      wx.showModal({
                                          title: '提示',
                                          content: '获取权限成功',
                                          showCancel: false,
                                      })
                                  } else {
                                      wx.showModal({
                                          title: '提示',
                                          content: '获取权限失败，将无法保存到相册哦~',
                                          showCancel: false,
                                      })
                                  }
                              },
                          })
                      }
                  })
              }

          }
      })
  },
  shareFriend(){
      wx.canvasToTempFilePath({     //将canvas生成图片
          canvasId: 'myCanvas',
          x: 0,
          y: 0,
          width: 370,
          height: 472,
          success: function (res) {
              let urls = res.tempFilePath   //图片临时路径
              wx.showShareImageMenu({  //分享给朋友
                  path: urls,
                  success: (res) => {
                      console.log("分享成功：", res);
                  },
                  fail: (err) => {
                      console.log("分享失败：", err);
                      wx.showToast({
                          title: "分享失败",
                          duration: 2000
                      })
                  },
              })
          }
      })
  }
})
